import {
	getToken,
	toast
} from '@/utils/util.js'

const baseUrl = 'http://api.codetime.com.cn'; // 正式接口地址
//const baseUrl = 'http://10.11.32.112:8098/'; // 测试地址

export const request = (url, params, type) => {
	return new Promise((reslove, reject) => {
		uni.showLoading({
			title: '加载中',
			mask: true
		})
		uni.request({
			url: baseUrl + url,
			method: type || 'POST',
			data: params,
			dataType: 'json',
			header: {
				'Authorization': uni.getStorageSync('axtoken') || {}
			},
			success: (resData) => {
				uni.hideLoading();
				if (resData.data.Code == 401 || resData.data.Code == 402) {
					uni.clearStorageSync();
					// toast('身份已过期,请重新登录')
					uni.navigateTo({
						url: '/pages/login/login'
					})
					reject(resData.data)
				} else if (resData.data.Code == 777) {
					uni.navigateTo({
						url: '/pages/vip/vip'
					})
				} else if (resData.data.Code == 0) {
					reslove(resData.data);
				} else {
					console.log(resData)
					reject(resData.data);
				}
			},
			fail: (errData) => {
				uni.hideLoading();
				reject(errData)
			}
		})
	})
}
